package com.helen.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.helen.entity.CouponsEntity;
import com.helen.shopping.R;
import com.helen.utils.MyLog;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;


/**
 * Created by yang on 2018/5/2.
 */

public class CouponsListAdapter extends BaseQuickAdapter<CouponsEntity, BaseViewHolder> {
    private boolean isCheckBox;
    private CouponsEntity couponsEntity;
    private int couponsId;

    public CouponsListAdapter(int layoutResId, List<CouponsEntity> data, boolean isCheckBox) {
        super(layoutResId, data);
        this.isCheckBox = isCheckBox;

    }

    public CouponsListAdapter(int layoutResId, List<CouponsEntity> data, boolean isCheckBox, CouponsEntity couponsEntity,int couponsId) {
        super(layoutResId, data);
        this.isCheckBox = isCheckBox;
        this.couponsEntity = couponsEntity;
        this.couponsId = couponsId;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final CouponsEntity item) {
        try {
            CircleImageView img = (CircleImageView) helper.getView(R.id.img);
            TextView tvName = (TextView) helper.getView(R.id.tv_name);
            TextView tvTime = (TextView) helper.getView(R.id.tv_get_time);
            TextView tvEndTime = (TextView) helper.getView(R.id.tv_end_time);
            TextView tvDiscount = (TextView) helper.getView(R.id.tv_discount_amount);
            final CheckBox cb = (CheckBox) helper.getView(R.id.cb);
            tvName.setText(item.getName());
            tvTime.setText("获得时间: " + item.getCreatetime());
            tvEndTime.setText("到期时间: "+item.getEndtime());
            tvDiscount.setText("￥" + item.getMoney());


            cb.setTag(helper.getAdapterPosition());
            if (couponsId != 0 && couponsId == item.getId()) {
                cb.setChecked(true);
            } else {
                cb.setChecked(false);
            }

            if (isCheckBox) {
                cb.setVisibility(View.VISIBLE);
                img.setVisibility(View.GONE);
                cb.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(cb.getTag().equals(helper.getAdapterPosition())){
                            if (((CheckBox) v).isChecked()) {
                                couponsEntity = item;
                                couponsId = item.getId();
                            } else {
                                couponsEntity = null;
                                couponsId = 0;
                            }
                        }
                        notifyDataSetChanged();
                    }
                });
            } else {
                cb.setVisibility(View.GONE);
                img.setVisibility(View.VISIBLE);
            }

        } catch (Exception e) {
            MyLog.e("yang", "设置优惠券列表异常" + e.toString());
        }


    }


    public CouponsEntity getCouponsEntity() {
        return couponsEntity;
    }

    public void setCouponsEntity(CouponsEntity couponsEntity) {
        this.couponsEntity = couponsEntity;
    }

    public int getCouponsId() {
        return couponsId;
    }

    public void setCouponsId(int couponsId) {
        this.couponsId = couponsId;
    }
}
